#include <iostream>
#include <iomanip>
#include <winsock2.h>
#include <iphlpapi.h>
#include "get_all_host_in_LAN.h"

#pragma comment(lib, "WS2_32.lib")
#pragma comment(lib, "IPHLPAPI.lib")

void display_mac_address(unsigned char *MacAddress, unsigned int MacAddress_len) {
	for(int i=0; i<MacAddress_len-1; i++)
		printf("%02x-", MacAddress[i]);
	printf("%02x", MacAddress[MacAddress_len-1]);
}

void main() {

	unsigned int IPAddress_len = 4;

	char IPAddress[] = "202.114.215.163";
	get_host_IPAddress(IPAddress);
	std::cout << "IPAddress " << IPAddress << std::endl;

	char IPMask[] = "255.255.255.000";
	get_host_IPMask(IPMask);
	std::cout << "IPMask " << IPMask << std::endl;

	int MacAddress_len = 8;
	unsigned char *MacAddress = new unsigned char[MacAddress_len];
	if(get_host_MacAddress(MacAddress, MacAddress_len) == ERROR_BUFFER_OVERFLOW) {
		delete MacAddress;
		MacAddress = new unsigned char[MacAddress_len];
	}
	get_host_MacAddress(MacAddress, MacAddress_len);
	std::cout << "MacAddress ";
	display_mac_address(MacAddress, MacAddress_len);
	std::cout << std::endl;

	std::vector<unsigned int> possible_IP;
	get_all_possible_IP_in_LAN(possible_IP);
	std::vector<unsigned int>::iterator possible_IP_i;
	in_addr possible_addr;
	std::cout << std::endl;
	unsigned int dstMacAddress_len = 8;
	unsigned char *dstMacAddress = new unsigned char[dstMacAddress_len];
	for(possible_IP_i=possible_IP.begin(); possible_IP_i!=possible_IP.end(); possible_IP_i++) {
		possible_addr.s_addr = *possible_IP_i;
		dstMacAddress_len = 8;
		if(get_MacAddress_of_particular_IPAddress(*possible_IP_i, dstMacAddress, dstMacAddress_len) == 0) {
			std::cout << inet_ntoa(possible_addr) << "           ";
			display_mac_address(dstMacAddress, dstMacAddress_len);
			std::cout << std::endl;
		} else {
			std::cout << inet_ntoa(possible_addr) << "           ";
			std::cout << "Shut down";
			std::cout << std::endl;
		}		
	}
	delete []dstMacAddress;

	/*unsigned char *dstMacAddress = new unsigned char[MacAddress_len];
	dstMacAddress[0] = 0x00;
	dstMacAddress[1] = 0x1f;
	dstMacAddress[2] = 0x3a;
	dstMacAddress[3] = 0x11;
	dstMacAddress[4] = 0x26;
	dstMacAddress[5] = 0x85;
	unsigned int uidstIPAddress;
	display_mac_address(dstMacAddress, MacAddress_len);
	std::cout << std::endl;
	in_addr addr;
	if(get_IPAddress_of_particular_MacAddress(uidstIPAddress, dstMacAddress, MacAddress_len) == 0) {
		addr.s_addr = uidstIPAddress;
		std::cout << "           " << inet_ntoa(addr) << std::endl;
	} else {
		std::cout << "           " << "not found" << std::endl;
	}
	delete dstMacAddress;*/
	
	delete []MacAddress;

	system("pause");
}
